﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Share.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RemoteCtrls.ActivityToolBox.Repository
{
    internal static class RecordRepository
    {
        private const string ProcessGo = "ProcessGo";

        private const string Studio = "Studio";

        private const string RecentActivities = "recents.yml";

        private const string FavoriteActivities = "favorites.yml";
        /// <summary>
        /// 获取配置文件最近的活动
        /// </summary>
        /// <returns></returns>
        public static List<RecentRecord> GetRecentActivities()
        {
            var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var filePath = System.IO.Path.Combine(localAppData, ProcessGo, Studio, RecentActivities);

            List<RecentRecord> records = null;
            if (File.Exists(filePath))//检查配置文件是否存在，如果存在这加载，否者新建。
            {
                records = YamlHelper.Load<List<RecentRecord>>(filePath, true);
            }
            else
            {
                records = new List<RecentRecord>();
                SaveRecentActivities(records);
            }

            return records;
        }
        /// <summary>
        /// 保存最近的活动
        /// </summary>
        /// <param name="activities"></param>
        public static void SaveRecentActivities(List<RecentRecord> activities)
        {
            var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var sysPath = System.IO.Path.Combine(localAppData, ProcessGo, Studio);

            if (!Directory.Exists(sysPath))
            {
                Directory.CreateDirectory(sysPath);
            }

            var filePath = System.IO.Path.Combine(sysPath, RecentActivities);
            YamlHelper.Save(activities, filePath, true);
        }
        /// <summary>
        /// 获取配置文件中收藏的活动
        /// </summary>
        /// <returns></returns>
        public static List<FavoriteRecord> GetFavoriteActivities()
        {
            var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var filePath = System.IO.Path.Combine(localAppData, ProcessGo, Studio, FavoriteActivities);

            List<FavoriteRecord> records = null;
            if (File.Exists(filePath))//检查配置文件是否存在，如果存在这加载，否者新建。
            {
                records = YamlHelper.Load<List<FavoriteRecord>>(filePath, true);
            }
            else
            {
                records = new List<FavoriteRecord>();
                SaveFavoriteActivities(records);
            }

            return records;
        }
        /// <summary>
        /// 保存收藏活动
        /// </summary>
        /// <param name="activities"></param>
        public static void SaveFavoriteActivities(List<FavoriteRecord> activities)
        {
            var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var sysPath = System.IO.Path.Combine(localAppData, ProcessGo, Studio);

            if (!Directory.Exists(sysPath))
            {
                Directory.CreateDirectory(sysPath);
            }

            var filePath = System.IO.Path.Combine(sysPath, FavoriteActivities);
            YamlHelper.Save(activities, filePath, true);
        }
    }
}
